<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <title>权限</title>
    <link rel="stylesheet" th:href="@{/img/iconfont/authority/iconfont.css}">
    <link rel="stylesheet" th:href="@{/css/admin/authority.css}">
</head>
<body>

<!-- 结构 -->
<ul id="groupRule"></ul>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/js/authority.js}"></script>
<script type="text/javascript" th:inline="javascript">

    var context = $("meta[name='ctx']").attr("content") || '';

    var roleId = [[${roleId}]];

    $(function () {
        var data = [];
        var url = context + "/module/listModuleTree";
        if (roleId) {
            url += '?roleId=' + roleId;
        }

        //权限列表
        $.ajax({
            type: "POST",
            url: url,
            success: function (obj) {
                var code = obj.code;
                if (code == 0) {
                    data = obj.data;

                    console.log("data:"+ data);

                    detailData(data);

                    // 调用groupRule方法
                    $("#groupRule").groupRule({data: data});
                }
            }
        });

        // 数据处理函数
        function detailData(data) {
            var s_true = "s_true";
            var len = data.length;

            for (var i = 0; i < len; i++) {
                var childs1 = data[i].children;
                if (childs1.length > 0) {

                    for (var j = 0; j < childs1.length; j++) {
                        var childs2 = childs1[j].children;
                        var len2 = childs2.length;
                        if (len2 > 0) {
                            var sum2 = 0;
                            for (var k = 0; k < len2; k++) {
                                if (childs2[k].checked == true) {
                                    sum2++
                                }
                            }
                            if (sum2 === len2) {
                                childs1[j].checked = true;
                            } else if (sum2 == 0) {
                                childs1[j].checked = false;
                            } else {
                                childs1[j].checked = "s_true";
                            }
                            sum2 = 0;
                        }
                    }

                    var len1 = childs1.length;
                    var sum1 = 0;
                    var sum3 = 0;
                    for (var m = 0; m < len1; m++) {
                        if (childs1[m].checked == true) {
                            sum1++
                        } else if (childs1[m].checked == s_true) {
                            sum3++;
                        }
                    }
                    if (sum1 === len1) {
                        data[i].checked = true;
                    } else if (sum1 == 0 && sum3 == 0) {
                        data[i].checked = false;
                    } else {
                        data[i].checked = s_true;
                    }
                    sum1 = 0;
                    sum3 = 0;
                    childs1 = '';
                }
            }
            return data
        }
    });

    // 获取树形控件数据
    function getTreeData() {
        var checked = $("#groupRule").find(".checked")
        var len = $("#groupRule").find(".checked").length;
        console.log(len)
        if (checked.length <= 0) {
            console.log("请勾选选项")
        }
        var i = 0,
        arr = [],
        name = [];
        $(checked).each(function () {
            arr[i] = $(this).attr("id");
            name[i] = $(this).parents('.btn-toggle').find('span[class="label-text"]').html();
            i++;
        });
        var ids = arr.join(",");
        var names = name.join(",");
        return {
            idsdata: ids,
            namedata: names
        };
    }
</script>
</body>
</html>